var codeReg = /^[0-9]{8}$/;
var pwdReg= /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$/;

var $code = $("#code");
var $pwd1 = $("#pwd1");
var $pwd2 = $("#pwd2");
var $eyeOpen = $(".js-eye-open");
var flagcode = false;//公共flag 判断是不是验证通过
var flagpwd1 = false;//公共flag 判断是不是验证通过
var flagpwd2 = false;//公共flag 判断是不是验证通过

(function(){
	
    //查看明文密码
    $eyeOpen.on("click", lookPwd);  
    
    $("#checkBtn").on("click", submit);

    //手机号输入框事件
    $code.on("onreadystatechange input blur", function(){
      var $this = $(this);
      var code = $this.val();

      flagcode = validateForm($this, code, codeReg, "请输入正确的参赛证号");
    });

    //密码输入框事件
    $pwd1.on("onreadystatechange input blur", function(){
      var $this = $(this);
      var $cleartext = $this.next("input[type='text']");
      var pwd = $this.val();

      $cleartext.val(pwd);

      flagpwd1 = validateForm($this, pwd, pwdReg, "请输入正确的密码", $cleartext);
    });

    //明文密码输入框事件
    $pwd2.on("onreadystatechange input blur", function(){
        var $this = $(this);
        var $cleartext = $this.prev("input[type='password']");
        var pwd = $this.val();

        $cleartext.val(pwd);

        flagpwd2 = validateForm($this, pwd, pwdReg, "请输入正确的密码", $cleartext);
    });

    //表单验证, 前面四个都是必传参数：当前输入框、当前值、正则、提示文字，最后一个参数是可选参数：查看明文密码
    function validateForm(that, value, reg, txt, cleartext){
          var $parent = that.closest(".form-group");
          var $addon = $parent.find(".input-group-addon");
          var $msg = $parent.find(".js-line-alert");
          var $msgText = $parent.find(".js-alert-text");
          
          var flag = true;

          if(value.length === 0){
            modifyWarning($msgText, txt);
            showWarning($addon, that, $msg, cleartext);
            flag = false;
          }
          else if(!reg.test(value)){
            modifyWarning($msgText, txt);
            showWarning($addon, that, $msg, cleartext);
            flag = false;
          }
          else{
            hideWarning($addon, that, $msg, cleartext);
          }

          return flag;
    }

    //输入框边框变红并显示提示文字
    function showWarning(addon, input, msg, cleartext){
        if(cleartext){
          cleartext.addClass("input-error border-error");
        }
        addon.addClass("border-error");
        input.addClass("input-error border-error");
        msg.show();
    }

    //输入框边框恢复原色并隐藏提示文字
    function hideWarning(addon, input, msg, cleartext){
        if(cleartext){
          cleartext.removeClass("input-error border-error");
        }
        addon.removeClass("border-error");
        input.removeClass("input-error border-error");
        msg.hide();
    }

    //修改提示文字信息
    function modifyWarning(element, txt){
        element.text(txt);
    }

    //查看明文密码
      function lookPwd(){
        var $pwd = $(this).closest(".input-group").find("input[type='password']");
        var $pwd_show = $pwd.next("input[type='text']");
        var pwd = $pwd.val();
        var pwd_show = $pwd_show.val();

        if($pwd.is(":visible")){
            $pwd.hide();
            $pwd_show.val(pwd).show();
        }
        else{
            $pwd.val(pwd_show).show();
            $pwd_show.hide();
        }
      }

	//表单登录提交
    function submit(){
			var code = $code.val();
			var pwd = $pwd1.val();
			var gameId = $("#gameId").val();

    $code.blur();
    $pwd1.blur();
    $pwd2.blur();

		if(flagcode && flagpwd1 && flagpwd2){
			$.ajax({
            	url: "validate",
            	type: "post",
            	dataType: 'json',
            	data : {
					gameId:gameId,
            		code:code,
            		pwd:pwd
            	},
            	success: function(data) {
            		if (data.returnFlag == '0') {
            			location.href = "infor?permitId="+data.permitId;
            		}else {
      						if (data.returnCode == 'A006') {
                                layer.open({
                                    content: '报名已结束',
                                    scrollbar: false
                                });
      						}else if (data.returnCode == 'A008') {
                            layer.open({
                                content: '报名未开始',
                                scrollbar: false
                            });
                  }else if (data.returnCode == 'A001') {
                                layer.open({
                                    content: '该帐号已报名，请去个人中心查看',
                                    scrollbar: false
                                });
      						}else if (data.returnCode == 'A003') {
                                layer.open({
                                    content: '参赛证号已被使用',
                                    scrollbar: false
                                });
      						}else if (data.returnCode == 'A004') {
                                layer.open({
                                    content: '参赛证号和密码不匹配',
                                    scrollbar: false
                                });
      						}else if (data.returnCode == 'A005') {
                                layer.open({
                                    content: '该帐号报名已超过最大报名次数',
                                    scrollbar: false
                                });
      						}else {
                                layer.open({
                                    content: '无此参赛证号',
                                    scrollbar: false
                                });
      						}
            		}
            	},
            	error: function(xhr, type, errorThrown) {
            		//异常处理；
					alert("请稍后再试!");
            	}
			});
		}
    }

})();